﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace DataConvertLib
{
	/// <summary>
	/// UShort类型转换
	/// </summary>
	// Token: 0x02000018 RID: 24
	[Description("UShort类型数据转换类")]
	public class UShortLib
	{
		/// <summary>
		/// 字节数组中截取转成16位无符号整型
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="start">开始索引</param>
		/// <param name="dataFormat">数据格式</param>
		/// <returns>返回UShort结果</returns>
		// Token: 0x060000D6 RID: 214 RVA: 0x00005900 File Offset: 0x00003B00
		[Description("字节数组中截取转成16位无符号整型")]
		public static ushort GetUShortFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
		{
			byte[] value2 = ByteArrayLib.Get2BytesFromByteArray(value, start, dataFormat);
			return BitConverter.ToUInt16(value2, 0);
		}

		/// <summary>
		/// 将字节数组中截取转成16位无符号整型数组
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="type">数据格式</param>
		/// <returns>返回UShort数组</returns>
		// Token: 0x060000D7 RID: 215 RVA: 0x00005924 File Offset: 0x00003B24
		[Description("将字节数组中截取转成16位无符号整型数组")]
		public static ushort[] GetUShortArrayFromByteArray(byte[] value, DataFormat type = DataFormat.ABCD)
		{
			bool flag = value == null;
			if (flag)
			{
				throw new ArgumentNullException("检查数组长度是否为空");
			}
			bool flag2 = value.Length % 2 != 0;
			if (flag2)
			{
				throw new ArgumentNullException("检查数组长度是否为偶数");
			}
			ushort[] array = new ushort[value.Length / 2];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = UShortLib.GetUShortFromByteArray(value, i * 2, type);
			}
			return array;
		}

		/// <summary>
		/// 将字符串转转成16位无符号整型数组
		/// </summary>
		/// <param name="value">带转换字符串</param>
		/// <param name="spilt">分割符</param>
		/// <returns>返回Short数组</returns>
		// Token: 0x060000D8 RID: 216 RVA: 0x00005994 File Offset: 0x00003B94
		[Description("将字符串转转成16位无符号整型数组")]
		public static ushort[] GetUShortArrayFromString(string value, string spilt = " ")
		{
			value = value.Trim();
			List<ushort> list = new List<ushort>();
			ushort[] result;
			try
			{
				bool flag = value.Contains(spilt);
				if (flag)
				{
					string[] array = value.Split(new string[]
					{
						spilt
					}, StringSplitOptions.RemoveEmptyEntries);
					foreach (string text in array)
					{
						list.Add(Convert.ToUInt16(text.Trim()));
					}
				}
				else
				{
					list.Add(Convert.ToUInt16(value.Trim()));
				}
				result = list.ToArray();
			}
			catch (Exception ex)
			{
				throw new ArgumentNullException("数据转换失败：" + ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 设置字节数组某个位
		/// </summary>
		/// <param name="value">字节数组</param>
		/// <param name="offset">偏移位</param>
		/// <param name="bitVal">True或者False</param>
		/// <param name="dataFormat">数据格式</param>
		/// <returns>返回UShort结果</returns>
		// Token: 0x060000D9 RID: 217 RVA: 0x00005A4C File Offset: 0x00003C4C
		[Description("设置字节数组某个位")]
		public static ushort SetBitValueFrom2ByteArray(byte[] value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
		{
			bool flag = offset >= 0 && offset <= 7;
			if (flag)
			{
				value[1] = ByteLib.SetbitValue(value[1], offset, bitVal);
			}
			else
			{
				value[0] = ByteLib.SetbitValue(value[0], offset - 8, bitVal);
			}
			return UShortLib.GetUShortFromByteArray(value, 0, dataFormat);
		}

		/// <summary>
		/// 设置16位整型某个位
		/// </summary>
		/// <param name="value">Short数据</param>
		/// <param name="offset">偏移位</param>
		/// <param name="bitVal">True或者False</param>
		/// <param name="dataFormat">数据格式</param>
		/// <returns>返回UShort结果</returns>
		// Token: 0x060000DA RID: 218 RVA: 0x00005A9C File Offset: 0x00003C9C
		[Description("设置16位整型某个位")]
		public static ushort SetBitValueFromUShort(ushort value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
		{
			byte[] byteArrayFromUShort = ByteArrayLib.GetByteArrayFromUShort(value, dataFormat);
			return UShortLib.SetBitValueFrom2ByteArray(byteArrayFromUShort, offset, bitVal, dataFormat);
		}

		/// <summary>
		/// 通过布尔长度取整数
		/// </summary>
		/// <param name="boolLength">布尔长度</param>
		/// <returns>整数</returns>
		// Token: 0x060000DB RID: 219 RVA: 0x00005AC0 File Offset: 0x00003CC0
		[Description("通过布尔长度取整数")]
		public static ushort GetByteLengthFromBoolLength(int boolLength)
		{
			return (boolLength % 8 == 0) ? ((ushort)(boolLength / 8)) : ((ushort)(boolLength / 8 + 1));
		}
	}
}
